package com.giantflyingsaucer.blog.greentea.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;

public class SimpleRPCEntry implements EntryPoint {

	private final SimpleServiceAsync simpleService = GWT.create(SimpleService.class);
	
	@Override
	public void onModuleLoad() {
		// TODO Auto-generated method stub
		final HorizontalPanel horzPanel = new HorizontalPanel();
		final Button submitButton = new Button("Submit");
		final TextBox number1Textbox = new TextBox();
		final TextBox number2Textbox = new TextBox();
		final Label messageLabel = new Label();
		final Label additionLabel = new Label("+");

		horzPanel.add(number1Textbox);
		horzPanel.add(additionLabel);
		horzPanel.add(number2Textbox);
		horzPanel.add(submitButton);
		horzPanel.add(messageLabel);
		 
		submitButton.addClickHandler(new ClickHandler() {
		    @Override
		    public void onClick(ClickEvent event)
		    {
		    	  simpleService.handleSimpleRequest(Integer.parseInt(number1Textbox.getText()), Integer.parseInt(number2Textbox.getText()), new AsyncCallback<Integer>() {
	                    @Override      
	                    public void onFailure(Throwable caught)
	                    {   
	                        messageLabel.setText(caught.getMessage());
	                    }
	 
	                    @Override
	                    public void onSuccess(Integer result)
	                    {
	                        messageLabel.setText(String.valueOf(result));
	                    }
	                }); 
		    }
		});
		RootPanel.get().add(horzPanel);
	}
}
